import psutil


# CPU内核数
def cpuNumber():
    return psutil.cpu_count()


# CPU使用率
def cpuUsageRate():
    allusage = psutil.cpu_percent(interval=1, percpu=True)
    usagerate = 0
    for i in allusage:
        usagerate += i
    usagerate = usagerate / len(allusage)
    return usagerate


# 内存使用率
def memoryUsageRate():
    return psutil.virtual_memory().percent


def overallPerformance():
    first = cpuUsageRate()
    second = memoryUsageRate()
    performance = 0.7 * first + 0.3 * second
    return performance


def initParameter():
    performance = overallPerformance()
    cpuNum = cpuNumber()
    return performance, cpuNum

# class sendThread(threading.Thread):
#     def __init__(self, sock):
#         threading.Thread.__init__(self)
#         self.sock = sock
#
#     def run(self):
#         global flag
#         while not flag:
#             self.sock.sendto('start'.encode('utf-8'), ('255.255.255.255', 2201))
#             print(flag)
#             time.sleep(1)
#
#
# class receiveThread(threading.Thread):
#     def __init__(self, sock):
#         threading.Thread.__init__(self)
#         self.sock = sock
#         self.lock = threading.Lock()
#
#     def run(self):
#         # print(id(self.flag))
#         global flag
#         while True:
#             data, addr = self.sock.recvfrom(1024)
#             self.lock.acquire()
#             flag = 1
#             self.lock.release()
#             print(flag)
#             print(1)
#             # print(data.decode('utf-8'))
#             # str = data.decode('utf-8').split(',')
#             # str[1] = str[1].strip('(')
#             # str[1] = str[1].strip("'")
#             # print(str[1])
#             # str[2] = str[2].strip(')')
#             # address = (str[1], int(str[2]))
#             # print(address)
#             # if str[0] == 'ACK':
#             #     flag = False
#             # self.sock.close()
#

#
# def tcplink():
#     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     s.bind(('0.0.0.0', 2300))
#     s.listen(4)
#     print("waitting for connection!")
#     sock, addr = s.accept()
#     print("收到来自%s:%s的连接" % addr)
#     sock.send(b'welcome!')
#     time.sleep(15)
#     data = sock.recv(1024)
#     print(data.decode('utf-8'))
#     time.sleep(1)
#     sock.send("hello!".encode('utf-8'))
#     sock.close
#     print("连接中断")
#
#
# # if __name__=="__main__":
# #     tcplink()
# def broadcastServer():
#     flag = True
#     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
#     sock.bind(('', 2101))
#     while True:
#         data, addr = sock.recvfrom(1024)
#         print(data.decode('utf8'))
#         if data.decode('utf-8') == 'ACK':
#             print('停止发送数据')
#             sock.sendto('ACK'.encode('utf-8'), addr)
#             break
#         sock.sendto('ACK'.encode('utf-8'), addr)
#         time.sleep(1)
#         sock.close
#
#
# def tellAllHost():
#     flag = True
#     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#     sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
#     sock.settimeout(20)
#     sock.bind(('0.0.0.0', 2200))
#     # while  flag:
#     #     sock.sendto('start'.encode('utf-8'), (('255.255.255.255'),2201))
#     while True:
#         sock.sendto('start'.encode('utf-8'), (('255.255.255.255'), 2201))
#         data, addr = sock.recvfrom(1024)
#         print(data.decode('utf-8'))
#         str = data.decode('utf-8').split(',')
#         str[1] = str[1].strip('(')
#         str[1] = str[1].strip("'")
#         print(str[1])
#         str[2] = str[2].strip(')')
#         address = (str[1], int(str[2]))
#         print(address)
#         if str[0] == 'ACK':
#             flag = False
#         sock.close()
#         return address

# if __name__ == "__main__":
#     # print(cpuNumber())
#     # print(cpuUsageRate())
#     # print(memoryUsageRate())
#     # print(overallPerformance())
#     # print(float(overallPerformance()))
#     performance, cpuNum = initParameter()
#     print(performance + "" + cpuNum)
# def connecttcp():
#     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     s.connect(('127.0.0.1',2300))
#     data = s.recv(1024)
#     print(data.decode('utf-8'))
#     s.send('i am first'.encode('utf-8'))
#     data = s.recv(1024)
#     print(data.decode('utf-8'))
